import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

let login = () => import("../pages/login/login.vue");
let index = () => import("../pages/index/index.vue");
let register = () => import("../pages/register/register.vue");
let list = () => import("../pages/list/list.vue");
let order = () => import("../pages/order/order.vue");
let address = () => import("../pages/address/address.vue");
let payment = () => import("../pages/payment/payment.vue");
let newaddress = () => import("../pages/newaddress/newaddress.vue");

let home = () => {
  return Promise.resolve(import("../pages/home/home.vue"))
}
let cate = () => Promise.resolve(import("../pages/cate/cate.vue"));
let shop = () => Promise.resolve(import("../pages/shop/shop.vue"));
let mine = () => Promise.resolve(import("../pages/mine/mine.vue"));
const routes = [
  // 一级路由
  {
    path:"/login",
    component: login,
    meta: {
      title: "登录"
    }
  },
  {
    path:"/register",
    component: register,
    meta: {
      title: "注册",
      goBack:true
    }
  },
  {
    path:"/list",
    component: list,
    meta: {
      title: "列表",
      goBack:true
    }
  },
  {
    path:"/detail/:id",
    // path:"/detail",
    component: ()=>import("../pages/detail/detail.vue"),
    meta: {
      title: "商品详情",
      goBack:true
    }
  },
  {
    path:"/search",
    component: ()=>import("../pages/search/search.vue"),
    meta: {
      title: "搜素",
      goBack:true
    }
  },
  {
    path:"/order",
    component: order,
    meta:{
      title:"确认订单",
      goBack:true
    }
  },
  {
    path:"/address",
    component: address,
    meta:{
      title:"地址管理",
      goBack:true
    }
  },
  {
    path:"/payment",
    component: payment,
    meta:{
      title:"支付订单",
      goBack:true
    }
  },
  {
    path:"/newaddress",
    component: newaddress,
    meta:{
      title:"新增地址",
      goBack:true
    }
  },
  {
    path:"/index",
    component: index,
    // 二级路由
    children: [
      {
      path:"home",
      component:home,
      meta: {
        title: "小U首页"
      }
    },
      {
      path:"cate",
      component:cate,
      meta: {
        title: "分类"
      }
    },
      {
      path:"shop",
      component:shop,
      meta: {
        title: "购物车"
      }
    },
      {
      path:"mine",
      component:mine,
      meta: {
        title: "我的"
      }
    },
    // 二级重定向
      {
      path:"",
      redirect:"home",
    },
  ]
  },
  // 一级重定向
  {
    path:"*",
    redirect:"/login"
  }
]

const router = new VueRouter({
  // 路由规则
  routes,
  // 滚动行为
  scrollBehavior(to,from,savePosition){
    if(savePosition){
      // 如果保存了上一次位置就返回上一次的位置 没有保存就返回 0,0
      return savePosition;
    }else{
      return {
        x:0,
        y:0
      }
    }
  }
})

// 全局前置守卫
router.beforeEach((to,from,next)=>{
  // console.log("----全局前置守卫----");
  //如果是登录或者注册 直接进入
  if(to.path == "/login" || to.path == "/register"){
    next();
    return
  }
  //不是登录注册 判断是不是登录了
  let isLogin = localStorage.getItem("isLogin");
  if(isLogin){
    next();
    return;
  }
  // 没有登录 进入登录页面
  next("/login")
})

// 全局后置守卫 所有路由进入之后
router.afterEach((to,from)=>{
  // console.log("--------全局后置守卫-------");
})
export default router
